package com.stylefeng.guns.modular.manager.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.shiro.ShiroKit;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.ManagerCustomer;
import com.stylefeng.guns.modular.manager.service.IManagerCustomerService;

import java.util.Date;
import java.util.Objects;

/**
 * 下游客户管理控制器
 *
 * @author fengshuonan
 * @Date 2018-09-12 20:22:22
 */
@Controller
@RequestMapping("/managerCustomer")
public class ManagerCustomerController extends BaseController {

    private String PREFIX = "/manager/managerCustomer/";

    @Autowired
    private IManagerCustomerService managerCustomerService;

    /**
     * 跳转到下游客户管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "managerCustomer.html";
    }

    /**
     * 跳转到添加下游客户管理
     */
    @RequestMapping("/managerCustomer_add")
    public String managerCustomerAdd() {
        return PREFIX + "managerCustomer_add.html";
    }

    /**
     * 跳转到修改下游客户管理
     */
    @RequestMapping("/managerCustomer_update/{managerCustomerId}")
    public String managerCustomerUpdate(@PathVariable Integer managerCustomerId, Model model) {
        ManagerCustomer managerCustomer = managerCustomerService.selectById(managerCustomerId);
        model.addAttribute("item",managerCustomer);
        LogObjectHolder.me().set(managerCustomer);
        return PREFIX + "managerCustomer_edit.html";
    }

    /**
     * 获取下游客户管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(ManagerCustomer managerCustomer) {
        Wrapper entityWrapper = Condition.wrapper()
                .like("customer_name", managerCustomer.getCustomerName())
                .like("customer_address", managerCustomer.getCustomerAddress());
        return managerCustomerService.selectList(entityWrapper);
    }

    /**
     * 新增下游客户管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ManagerCustomer managerCustomer) {
        String createBy = Objects.requireNonNull(ShiroKit.getUser()).getAccount();
        managerCustomer.setCreateBy(createBy);
        managerCustomer.setUpdateBy(createBy);
        managerCustomerService.insert(managerCustomer);
        return SUCCESS_TIP;
    }

    /**
     * 删除下游客户管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer managerCustomerId) {
        managerCustomerService.deleteById(Long.valueOf(managerCustomerId));
        return SUCCESS_TIP;
    }

    /**
     * 修改下游客户管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(ManagerCustomer managerCustomer) {
        String createBy = Objects.requireNonNull(ShiroKit.getUser()).getAccount();
        managerCustomer.setUpdateBy(createBy);
        managerCustomer.setUpdateDate(new Date());
        managerCustomerService.updateById(managerCustomer);
        return SUCCESS_TIP;
    }

    /**
     * 下游客户管理详情
     */
    @RequestMapping(value = "/detail/{managerCustomerId}")
    @ResponseBody
    public Object detail(@PathVariable("managerCustomerId") Integer managerCustomerId) {
        return managerCustomerService.selectById(managerCustomerId);
    }
}
